#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKeyboard.h>
#import <LockInfo/Plugin.h>

#import "framework/FWApplication.h"
#import "framework/FWPlugin.h"
#import "framework/FWCommon.h"
#import "framework/FWPreferences.h"

#import "PINewTodo.h"
#import "PITask.h"
#import "PITaskIntegration.h"
#import "PICommon.h"


@implementation PINewTodo

@synthesize importance;


// ******************************************************************************************
#pragma mark -
#pragma mark Initialisation and object management

- (id)initWithFrame:(CGRect)rect application:(FWApplication*)appl preferences:(FWPreferences*)prefs
{
    if ((self = [super initWithFrame:rect caption:@"New Task/Title" label:@"New Task/Name" prompt:@"New Task/Name Prompt" application:appl preferences:prefs])) {
        NSBundle* bundle = [NSBundle bundleForClass:[self class]];
        
        UISegmentedControl* navControl = [[[UISegmentedControl alloc]
                            initWithItems:
                            [NSArray arrayWithObjects:
                             localize(bundle, @"Importance/None"),
                             localize(bundle, @"Importance/Top"),
                             localize(bundle, @"Importance/High"),
                             localize(bundle, @"Importance/Normal"),
                             localize(bundle, @"Importance/Low"),
                             nil]] autorelease];
        navControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        navControl.segmentedControlStyle = UISegmentedControlStyleBar;
        navControl.selectedSegmentIndex = [prefs intValue:kPIOptionsDefaultImportance withDefault:kPIOptionsDefaultImportanceDefault];
        self.importance = navControl;
        
        CGRect textFrame = self.text.frame;
        if (FWIsDeviceAnIPad()) {
            navControl.frame = CGRectMake(textFrame.origin.x + 50,
                                          textFrame.origin.y + textFrame.size.height + 10,
                                          textFrame.size.width - 100,
                                          30);
        }
        else {
            navControl.frame = CGRectMake(textFrame.origin.x,
                                          textFrame.origin.y + textFrame.size.height + 10,
                                          textFrame.size.width,
                                          30);
        }
        
        [self addSubview:navControl];
    }
    
    return self;
}

- (void)dealloc
{
    [importance release];
    
    [super dealloc];
}


// ******************************************************************************************
#pragma mark -
#pragma mark Layout management - try to look reasonable on both iPhone and iPad

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGSize size = FWGetCurrentScreenSize();
    
    CGRect textFrame = self.text.frame;
    CGRect importanceFrame = self.importance.frame;
    
    if (size.height > size.width || FWIsDeviceAnIPad())
        importanceFrame.origin.y = textFrame.origin.y + textFrame.size.height + (FWIsDeviceAnIPad() ? 50 : 20);
    else
        // Not much room on a hiorizonal iPhone so squeeze things together!
        importanceFrame.origin.y = textFrame.origin.y + textFrame.size.height + 4;
    
    self.importance.frame = importanceFrame;
}


// ******************************************************************************************
#pragma mark -
#pragma mark Command button callbacks

- (void)doneClicked
{
    NSString* todoTitle = self.text.text;
    
    XLog(@"LI:PITodo: New todo to be created: %@", todoTitle);
    [self.application.fwPlugin.liPlugin dismissPreview];

    if (self.hasFired)
        return;
    else
        self.hasFired = YES;
    
    PIImportanceType importanceCode = PIImportanceNone;
    switch (self.importance.selectedSegmentIndex) {
        case 0:
            importanceCode = PIImportanceNone;
            break;
        case 1:
            importanceCode = PIImportanceTop;
            break;
        case 2:
            importanceCode = PIImportanceHigh;
            break;
        case 3:
            importanceCode = PIImportanceNormal;
            break;
        case 4:
            importanceCode = PIImportanceLow;
            break;
    }
    
    NSURL* url = [NSURL URLWithString:[PITaskIntegration newTaskURL:todoTitle
                                                         importance:importanceCode
                                                        application:self.application]];
    [self.application.fwPlugin.liPlugin launchURL:url];
}

@end
